package br.com.officium.util;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.application.NavigationHandler;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 * Classe utilitária para a camada de visão.
 *
 * @author Ivo
 */
public final class BeanUtil {

    /**
     * private constructor so others cannot instantiate
     */
    private BeanUtil() {
        throw new AssertionError();
    }

    public static void addInfo(String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", msg));
    }

    public static void addWarn(String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "", msg));
    }

    public static void addError(String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", msg));
    }

    public static void addFatal(String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "", msg));
    }

    public static void sessionDestroy() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
    }

    public static String getParam(String param) {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context != null) {
            return context.getExternalContext().getRequestParameterMap().get(param);
        }
        return null;
    }

    public static Object getSessionParam(String param) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
        return sessionMap.get(param);
    }

    public static void setSessionParam(String param, Object object) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
        sessionMap.put(param, object);
    }

    public static void removeSessionObject(String param) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
        sessionMap.remove(param);
    }

    public static Object getBean(String beanName) {

        FacesContext context = FacesContext.getCurrentInstance();
        Object bean = null;
        if (context != null) {
            bean = context.getExternalContext().getSessionMap().get(beanName);
        }
        return bean;
    }

    public static Object getBean(String beanName, FacesContext localContext) {
        Map<String, Object> sessionMap = localContext.getExternalContext().getSessionMap();
        Object bean = sessionMap.get(beanName);
        return bean;
    }

    public static void handleNavigation(String outcome, FacesContext localContext) {
        NavigationHandler nh = localContext.getApplication().getNavigationHandler();
        nh.handleNavigation(localContext, null, outcome);
    }

    public static void cleanSubmittedValues(UIComponent component) {
        if (component != null && component.getChildren() != null) {
            component.getChildren().clear();
        }
    }

    public static String retrieveRealPath(String file) {
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
        return servletContext.getRealPath(file);
    }

    public static String getDefaultResourceString(String key) {
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale = context.getApplication().getDefaultLocale();
        ResourceBundle bundle = ResourceBundle.getBundle(context.getApplication().getMessageBundle(), locale);
        return bundle.getString(key);
    }
    
}
